/*
    Copyright Nicolas Ronvel 2007
	gulix33xp@yahoo.fr

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

using System;
using System.Collections.Generic;
using System.Drawing;

using SdlDotNet.Graphics;
using SdlDotNet.Input;

namespace BlindShark.Widgets
{
	/// <summary>
	/// Description of ImageGallery.
	/// </summary>
	public class ImageGallery : IWidget
	{
		public delegate void SelectedIndexChangedEvent();
		public SelectedIndexChangedEvent SelectedIndexChanged;
		
		#region Variables
		Surface _sfGallery;
		
		List<string> _lsImages;
		int _iSelectedImage;
		
		Color _clrBack;
		Color _clrBorder;
		int _iBorderSize;
		Rectangle _rctGallery;
		Size _szCenterImage;
		Size _szSideImage;
		
		bool _bEnabled;
		#endregion
		
		public ImageGallery()
		{
			_lsImages = new List<string>();
			_iSelectedImage = 0;
			_clrBack = Color.Black;
			_clrBorder = Color.Black;
			_iBorderSize = 0;
			_bEnabled = true;
		}
		
		#region IWidget interface members
		public void Init()
		{
			BuildBackground();
			
			DisplayImages();
		}
		
		public void HandleEvents()
		{
			if (!_bEnabled)
				return;
			
			// Handling gallery events
			KeyboardState keyState = new KeyboardState();
			if ((_iSelectedImage>0) && keyState.IsKeyPressed(Key.RightArrow))
			{
				_iSelectedImage--;
				if (SelectedIndexChanged!=null)
					SelectedIndexChanged();
				Init();
			} else if ((_iSelectedImage<_lsImages.Count-1) && keyState.IsKeyPressed(Key.LeftArrow)) {
				_iSelectedImage++;
				if (SelectedIndexChanged!=null)
					SelectedIndexChanged();
				Init();
			}
		}
		
		public void CleanUp()
		{
			_sfGallery = null;
		}
		
		public Surface Surface
		{
			get {return _sfGallery;}
		}
		
		public Point Location
		{
			get {return _rctGallery.Location;}
			set {_rctGallery.Location = value;}
		}
		#endregion
		
		#region Displaying functions
		private void BuildBackground()
		{
			_sfGallery = new Surface(_rctGallery.Size);
			_sfGallery.Fill(_clrBorder);
			_sfGallery.Fill(new Rectangle(_iBorderSize,
			                              _iBorderSize,
			                              _rctGallery.Width - 2*_iBorderSize,
			                              _rctGallery.Height - 2*_iBorderSize),
			                _clrBack);
		}
		
		private void DisplayImages()
		{
			// Displaying the previous image
			if (_iSelectedImage>0)
			{
				Surface sfLeftPlayer = SurfaceManager.GetSurface(_lsImages[_iSelectedImage-1]);
				sfLeftPlayer = sfLeftPlayer.CreateStretchedSurface(_szSideImage);
				_sfGallery.Blit(sfLeftPlayer, new Point(10 + _iBorderSize,
				                                        (_rctGallery.Height - _szSideImage.Height) / 2));
			}
			
			// Displaying the next image
			if (_iSelectedImage<_lsImages.Count-1)
			{
				Surface sfRightPlayer = new Surface(_lsImages[_iSelectedImage+1]);
				sfRightPlayer = sfRightPlayer.CreateStretchedSurface(_szSideImage);
				_sfGallery.Blit(sfRightPlayer, 
				                new Point(_rctGallery.Width - _szSideImage.Width -  10 - _iBorderSize,
				                          (_rctGallery.Height - _szSideImage.Height) / 2));
			}
			
			// Displaying the selected image
			Surface sfSelectedPlayer = new Surface(_lsImages[_iSelectedImage]);
			sfSelectedPlayer = sfSelectedPlayer.CreateStretchedSurface(_szCenterImage);
			_sfGallery.Blit(sfSelectedPlayer, 
			                new Point((_rctGallery.Width - _szCenterImage.Width) / 2,
			                          (_rctGallery.Height - _szCenterImage.Height) / 2));
			
		}
		#endregion
		
		#region Accessors
		public bool Enabled
		{
			get {return _bEnabled;}
			set {_bEnabled = value;}
		}
		
		public int SelectedIndex
		{
			get {return _iSelectedImage;}
			set {_iSelectedImage = value;}
		}
		
		public Color BackColor
		{
			get {return _clrBack;}
			set {_clrBack = value;}
		}
		
		public Color BorderColor
		{
			get {return _clrBorder;}
			set {_clrBorder = value;}
		}
		
		public int BorderSize
		{
			get {return _iBorderSize;}
			set {_iBorderSize = value;}
		}
		
		public int X
		{
			get {return _rctGallery.X;}
			set {_rctGallery.X = value;}
		}
		
		public int Y
		{
			get {return _rctGallery.Y;}
			set {_rctGallery.Y = value;}
		}
		
		public Size Size
		{
			get {return _rctGallery.Size;}
			set {_rctGallery.Size = value;}
		}
		
		public int Width
		{
			get {return _rctGallery.Width;}
			set {_rctGallery.Width = value;}
		}
		
		public int Height
		{
			get {return _rctGallery.Height;}
			set {_rctGallery.Height = value;}
		}
		
		public Size CenterImageSize
		{
			get {return _szCenterImage;}
			set {_szCenterImage = value;}
		}
		
		public Size SideImageSize
		{
			get {return _szSideImage;}
			set {_szSideImage = value;}
		}
		
		public int Bottom
		{
			get {return _rctGallery.Bottom;}
		}
		
		public int Right
		{
			get {return _rctGallery.Right;}
		}
		#endregion
	
		#region List Functions
		public void Add(string imageFile)
		{
			_lsImages.Add(imageFile);
			if ((_sfGallery != null) && (_iSelectedImage == _lsImages.Count-2))
				Init();
		}
		
		public void AddRange(IEnumerable<string> imageFiles)
		{
			bool init = (_sfGallery != null) && (_iSelectedImage == _lsImages.Count-1);
			_lsImages.AddRange(imageFiles);
			if (init)
				Init();
		}
		#endregion
	}
}
